/*
 * Refer to the following URL which describes the programming specification for this file:
 * wiki goes here
 * 
 * TODO: Print out root directory (/usr/lib/carblogger_ui) to log file in /var.
 */

#include <Wt/WServer>
#include <Wt/WApplication>

#include "utilities.hpp"
#include "GUI.hpp"



int main(int argc, char *argv[])
{
    // Intercept argc and argv so I can make some required arguments, optional.      
    utilities::ModifiedArgv modified_argv(argc, argv);
    
    try
    {        
        Wt::WServer server(argv[0]);

        server.setServerConfiguration(modified_argv.getArgsSize(), modified_argv.getArgs(), WTHTTP_CONFIGURATION);
        
        /**
         * Add a resource here. i.e.
         * server.addResource(&rssFeed, "/wt/blog/feed/");
         */

        /*
         * Trying to make it possible to expand on this appliction:
         *  + Create a new entry point when needed.
        */
        server.addEntryPoint(Wt::Application,
                             /*
                              * Use a lambda function to reduce some code.
                              */
                             [=](const Wt::WEnvironment& env) -> Wt::WApplication*
                             {
                                 Wt::WApplication* app = new Wt::WApplication(env);
                                 
                                 /*
                                  * Style using style sheet
                                 */
                                 utilities::loadStylesheet("/css/global.css"); 
                                 /*
                                  * Or, the body could alternatively, be styled inline:
                                  * Wt::WApplication::instance()->styleSheet().addRule("body, html",
                                  *                                                    "margin: 0px;"
                                  *                                                    "padding:0;"
                                  *                                                    "height: 100%;"); 
                                  */
                                 
                                 // For now, use this to avoid warning (until scene gets used)
                                 new GUI(app->root()); 
                                 //Scene* scene = new Scene(app->root()); 
                                 
                                 // Next, use scene to render to
                                 //scene->left_top_write();
    
                                 return app;
                             },
                             "",
                             "/resources/icon.ico");
        
        if (server.start())
        {
            Wt::WServer::waitForShutdown();
            server.stop();
            
            return 0;
        }
    }
    catch (Wt::WServer::Exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    catch (std::exception &e)
    {
        std::cerr << "exception: " << e.what() << std::endl;
    }
}

